package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.util.DevLog;
import cn.sccl.common.util.SecurityUtils;
import cn.sccl.common.zabbix.Zabbix;
import cn.sccl.dirms.security.model.UserPrincipal;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 事件相关API
 * Created by juleiy on 2017/5/19.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/event")
public class EventController {


    /**
     * 添加事件消息
     */
    @RequestMapping(value = "/acknowledge", method = RequestMethod.POST)
    public void acknowledge(@RequestBody Map form, HttpServletRequest request) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<String, Object>();
        String[] triggerString = form.get("triggerids").toString().split(",");
        params.put("objectids",triggerString);
        params.put("output",new String[]{"eventid"});
        params.put("source",0);
        params.put("object",0);
        params.put("value",1);
        params.put("acknowledged",form.get("acknowledged"));


        //获取触发器下所有事件
        JSONObject result = zabbix.call("event.get", params);
        List<Map> eventArray = (List<Map>)result.get("result");
        List eventIdList = new ArrayList();
        for(Map m:eventArray){
            eventIdList.add(m.get("eventid"));
        }
        //备注所有事件
        params = new HashMap<String, Object>();
        params.put("eventids", eventIdList.toArray());
        UserPrincipal user = SecurityUtils.getUserPrincipal(request.getSession());
        String msg="";
        if(user!=null){
            msg = user.getName()+"：";
        }

        params.put("message", msg+form.get("message"));
        params.put("action", form.get("action"));//0 不关闭问题  1 关闭问题
        JSONObject acknowledgRresult = zabbix.call("event.acknowledge", params);
        System.out.println(acknowledgRresult.toJSONString());
    }

}
